package com.oracle.lingshangejia.activity.special;

import android.app.DatePickerDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.special.TNBArchivesExtendableListAdapter;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.dialog.LoadingViewDialog;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//糖尿病
public class TangNiaoBingActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private TNBArchivesExtendableListAdapter archivesExtendableListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tnb_add_archives_activity);
        initBasic();


        expandableListView = findViewById(R.id.expend_list);
        expandableListView.setGroupIndicator(null);
        String[] archivesStr = getResources().getStringArray(R.array.tnb_archives);
        archivesExtendableListAdapter = new TNBArchivesExtendableListAdapter(archivesStr);
        expandableListView.setAdapter(archivesExtendableListAdapter);
        // 添加末尾提交按钮
        View foot = LayoutInflater.from(this).inflate(R.layout.archives_foot_submit, null);
        expandableListView.addFooterView(foot);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;
            }
        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                int count = expandableListView.getExpandableListAdapter().getGroupCount();
                for (int i = 0; i < count; i++) {
                    if (i != groupPosition) {
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });
        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                View[] views = ((TNBArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
                View view = views[groupPosition];
                if (view == null) {
                    return;
                }
                Map<String, String> values = null;
                switch (groupPosition) {
                    /*case 0:
                        values = getBasicInfoValues();
                        break;*/
                    case 0:
                        values = Firsttable_1(view);
                        break;
                    case 1:
                        values = Firsttable_2(view);
                        break;
                    case 2:
                        values = Firsttable_3(view);
                        break;
                    case 3:
                        values = Firsttable_4(view);
                        break;
                    case 4:
                        values = Firsttable_5(view);
                        break;

                }

            }
        });

        expandableListView.expandGroup(0);

    }


    /**
     * 检查用户名、身份证号和本人联系电话是否正确填写
     *
     * @return 数据不正确返回true，正确返回false
     */
    private boolean checkBasicInfoIsError() {
        Map<String, String> basicInforValues = getBasicInfoValues();
        String xm = (String) basicInforValues.get("xm");
        // 姓名非空验证
        if (xm == null || xm.length() == 0) {
            Toast.makeText(getApplicationContext(), "姓名必须填写", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }





    /**
     * 获取基本信息数据
     */
    private Map<String, String> getBasicInfoValues() {
        View[] views = ((TNBArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
        View view = views[0];
        Map<String, String> params = new HashMap<>();
        String username;
        username= ((EditText) view.findViewById(R.id.diabetic_records_username)).getText().toString().trim();

        params.put("xm",username);
        return params;
    }

    /**
     * 第二页
     */
    private Map<String, String> Firsttable_1(View view) {
        Map<String, String> params = new HashMap<>();
        //居民id
       // String residentID="";
      //  residentID= ((EditText) view.findViewById(R.id.diabetic_records_1_ResidentID)).getText().toString().trim();

       // params.put("jm_id",residentID);
        // 随访日期
   /*     String followupdate = "";
        followupdate = ((TextView) view.findViewById(R.id.diabetic_records_1_Follow_up_date)).getText().toString().trim();
        params.put("sfrq", followupdate);*/
        // 随访方式
        String followupmode = "";
//        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Follow_up_mode)).isChecked()) {
//            followupmode = "1 ";
//        } else {
//            //national = ((EditText) views[0].findViewById(R.id.archives_national_other)).getText().toString().trim();
//            followupmode = "2";
//        }
        if (view != null) {
            RadioButton followupmode1 = view.findViewById(R.id.diabetic_records_1_Follow_up_mode);
            RadioButton followupmode2 = view.findViewById(R.id.diabetic_records_1_Follow_up_mode_1);
            RadioButton followupmode3 = view.findViewById(R.id.diabetic_records_1_Follow_up_mode_2);


            if (followupmode1.isChecked()) {
                String s = followupmode1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                followupmode  = s;
            } else if(followupmode2.isChecked()){
                String s = followupmode2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                followupmode = s;
            }else {
                String s = followupmode3.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                followupmode  = s;
            }
            //classificationText = ((EditText) view .findViewById(R.id.a_postpartum_housecall_classification_abnormalities_text)).getText().toString().trim();
        }
        params.put("sffs", followupmode); //随访方式
        // 症状
        String symptom = "";
        // 症状其它
        String othersymptom = "";
//        TableLayout table = ((TableLayout) view.findViewById(R.id.diabetic_records_1_Symptoms));
//        View[] cbs = getViews(table);
//        for (int i = 0; i < cbs.length; i++) {
//            if (cbs[i] instanceof CheckBox) {
//                CheckBox cb = (CheckBox) cbs[i];
//                if (cb.isChecked()) {
//                    String s = cb.getText().toString().trim();
//                    s = s.substring(0, s.indexOf(" "));
//                    symptom += s;
//                    othersymptom += ",";
//                }
//            }
//        }
//        if (symptom.length() != 0) {
//            symptom = symptom.substring(0, symptom.length() - 1);
//        }
//        othersymptom = ((EditText) view.findViewById(R.id.diabetic_records_1_Symptoms_other_text)).getText().toString().trim();

        View[] payViews = getViews(view.findViewById(R.id.diabetic_records_1_Symptoms));
        for (int i = 0; i < payViews.length; i++) {
            if (payViews[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) payViews[i];
                if (cb.isChecked()) {
                    String s = cb.getText().toString().trim();
                    s = s.substring(0, s.indexOf(" "));
                    symptom += s;
                    symptom += ",";
                }
            }
        }
        if (symptom.length() != 0) {
            symptom = symptom.substring(0, symptom.length() - 1);
        }
        othersymptom = ((EditText) view.findViewById(R.id.diabetic_records_1_Symptoms_other_text)).getText().toString().trim();

        params.put("tnbZz", symptom);//症状
        params.put("tnbZzQt", othersymptom);//其他



        // 体征血压
        String bloodpressure = "";
        // 体征血压_1
        String bloodpressure_1 = "";
        //体重
        String weight = "";
        //体质指数zuo
        String bodymassindex_zuo = "";
//        //体质指数you
//        String bodymassindex_you = "";
        //足背动脉搏动
        String pulsationofdorsalpedalartery = "";
        //其他
        String other = "";
        bloodpressure = ((TextView) view.findViewById(R.id.diabetic_records_1_Blood_pressure)).getText().toString().trim();
        bloodpressure_1 = ((TextView) view.findViewById(R.id.diabetic_records_1_Blood_pressure_1)).getText().toString().trim();

        weight = ((TextView) view.findViewById(R.id.diabetic_records_1_Body_weight_zou)).getText().toString().trim();
        bodymassindex_zuo = ((TextView) view.findViewById(R.id.diabetic_records_1_Body_mass_index_zuo)).getText().toString().trim();
//        bodymassindex_you = ((TextView) view.findViewById(R.id.diabetic_records_1_Body_mass_index_you)).getText().toString().trim();

        String tnbZbdmbdFx ="";//足背动脉方向
        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Pulsation_ok)).isChecked()) {
            //national = "1 汉族";
            pulsationofdorsalpedalartery = "1";
        } //diabetic_records_1_Pulsation_low
        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Pulsation_low)).isChecked()){
            pulsationofdorsalpedalartery = "2";
            tnbZbdmbdFx=((Spinner) view.findViewById(R.id.diabetic_records_1_Pulsation_low_xiala)).getSelectedItem().toString().trim();
        }
        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Pulsation_no)).isChecked()){
            tnbZbdmbdFx=((Spinner) view.findViewById(R.id.diabetic_records_1_Pulsation_low_xiala)).getSelectedItem().toString().trim();
            pulsationofdorsalpedalartery = "3";
        }


        other = ((TextView) view.findViewById(R.id.diabetic_records_1_sign_other)).getText().toString().trim();
        params.put("tnbTzXyss", bloodpressure);//血压收缩
        params.put("tnbTzXysz", bloodpressure_1);///血压舒张
        params.put("tnbTzTz", weight);//体重
        params.put("tnbTzTzzs", bodymassindex_zuo);//指标
//        params.put("tnb_tz_tzzs", bodymassindex_you);
        params.put("tnbZbdmbd",pulsationofdorsalpedalartery);//足被动脉
        params.put("tnbZbdmbdFx",tnbZbdmbdFx);//足背动脉方向
        params.put("tnbTzQt", other);//其他
        mValues.putAll(params);
        return params;
    }

    /**
     * 第三页
     */
    private Map<String, String> Firsttable_2(View view) {
        Map<String, String> params = new HashMap<>();
        // 日吸烟量
        String smoking_zou = "";
//        //日吸烟量2
//        String smoking_you = "";
        //日饮酒量
        String drinking_zou = "";
//        //日饮酒量2
//        String drinking_you = "";
        //运动次
        String shang_zou = "";
        //运动分钟
      String shang_you = "";
        //运动次2
        String xia_zou = "";
        //运动分钟2
//        String xia_you = "";
        //主食1
        String food_zou = "";
        //主食2
//        String food_you = "";
        smoking_zou = ((TextView) view.findViewById(R.id.diabetic_records_1_Daily_smoking_zou)).getText().toString().trim();
//        smoking_you = ((TextView) view.findViewById(R.id.diabetic_records_1_Daily_smoking_you)).getText().toString().trim();
        drinking_zou = ((TextView) view.findViewById(R.id.diabetic_records_1_Daily_drinking_zou)).getText().toString().trim();
//        drinking_you = ((TextView) view.findViewById(R.id.diabetic_records_1_Daily_drinking_you)).getText().toString().trim();
        shang_zou = ((TextView) view.findViewById(R.id.diabetic_records_1_movement_shang_zou)).getText().toString().trim();
        shang_you = ((TextView) view.findViewById(R.id.diabetic_records_1_movement_shang_you)).getText().toString().trim();
       // xia_zou = ((TextView) view.findViewById(R.id.diabetic_records_1_movement_xia_zou)).getText().toString().trim();
//        xia_you = ((TextView) view.findViewById(R.id.diabetic_records_1_movement_xia_you)).getText().toString().trim();
        food_zou = ((TextView) view.findViewById(R.id.diabetic_records_1_Staple_food_zou)).getText().toString().trim();
//        food_you = ((TextView) view.findViewById(R.id.diabetic_records_1_Staple_food_you)).getText().toString().trim();

        params.put("tnbShfszdRxyl", smoking_zou);//日吸烟
//        params.put("rxyly", smoking_you);
        params.put("tnbShfszdRyjl", drinking_zou);//日饮酒
//        params.put("ryjly", drinking_you);//
        params.put("tnbShfszdZyd", shang_zou);//运动次/周
        params.put("tnbShfszdCyd", shang_you);//           分钟次
      //  params.put("tnb_shfszd_cyd", xia_zou);//
//        params.put("ydxy", xia_you);//
        params.put("tnbShfszdZs", food_zou);//主食
//        params.put("zsy", food_you);//
        //心里调整
        String Psychological_adjustment = "";
//        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Psychological_adjustment)).isChecked()) {
//            //national = "1 汉族";
//            Psychological_adjustment = "1";
//        } else {
//            //national = ((EditText) views[0].findViewById(R.id.archives_national_other)).getText().toString().trim();
//            Psychological_adjustment = "2";
//        }
        if (view != null) {
            RadioButton Pa1 = view.findViewById(R.id.diabetic_records_1_Psychological_adjustment);
            RadioButton Pa2 = view.findViewById(R.id.diabetic_records_1_Psychological_adjustment_1);
            RadioButton Pa3 = view.findViewById(R.id.diabetic_records_1_Psychological_adjustment_2);


            if (Pa1.isChecked()) {
                String s = Pa1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Psychological_adjustment  = s;
            } else if(Pa2.isChecked()){
                String s = Pa2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Psychological_adjustment = s;
            }else {
                String s = Pa3.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Psychological_adjustment  = s;
            }
            //classificationText = ((EditText) view .findViewById(R.id.a_postpartum_housecall_classification_abnormalities_text)).getText().toString().trim();
        }
        params.put("tnbShfszdXltz", Psychological_adjustment);//心理调整
        //遵医行为
        String Compliance_behavior = "";
//        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Compliance_behavior)).isChecked()) {
//            //national = "1 汉族";
//            Compliance_behavior = "1";
//        } else {
//            //national = ((EditText) views[0].findViewById(R.id.archives_national_other)).getText().toString().trim();
//            Compliance_behavior = "2";
//        }
        if (view != null) {
            RadioButton Compliance_behavior1 = view.findViewById(R.id.diabetic_records_1_Compliance_behavior);
            RadioButton Compliance_behavior2 = view.findViewById(R.id.diabetic_records_1_Compliance_behavior_1);
            RadioButton Compliance_behavior3 = view.findViewById(R.id.diabetic_records_1_Compliance_behavior_2);


            if (Compliance_behavior1.isChecked()) {
                String s = Compliance_behavior1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Compliance_behavior  = s;
            } else if(Compliance_behavior2.isChecked()){
                String s = Compliance_behavior2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Compliance_behavior = s;
            }else {
                String s = Compliance_behavior3.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Compliance_behavior  = s;
            }
            //classificationText = ((EditText) view .findViewById(R.id.a_postpartum_housecall_classification_abnormalities_text)).getText().toString().trim();
        }
        params.put("tnbShfszdZyxw", Compliance_behavior);//遵医
        mValues.putAll(params);

        return params;
    }

    /**
     * 第四页
     */
    private Map<String, String> Firsttable_3(View view) {
        Map<String, String> params = new HashMap<>();
        //空腹血糖值
        String Fasting_plasma_glucose = "";
        // 其他检查
        String Auxiliary_examination_other = "";
        // 检查日期
        String date = "";
        Fasting_plasma_glucose = ((EditText) view.findViewById(R.id.diabetic_records_1_Fasting_plasma_glucose)).getText().toString().trim();
        Auxiliary_examination_other = ((EditText) view.findViewById(R.id.diabetic_records_1_Auxiliary_examination_other)).getText().toString().trim();
        date = ((TextView) view.findViewById(R.id.diabetic_records_1_doe)).getText().toString().trim();
        params.put("fzjcKfxtz", Fasting_plasma_glucose);//空腹血糖
        params.put("fzjcThxhdb", Auxiliary_examination_other);//f糖化血红蛋白
        params.put("fzjcDate", date);//检查日期
        //服药依从性
        String Medication_compliance = "";
        if (view != null) {
            RadioButton Mc1 = view.findViewById(R.id.diabetic_records_1_Medication_compliance);
            RadioButton Mc2 = view.findViewById(R.id.diabetic_records_1_Medication_compliance_1);
            RadioButton Mc3 = view.findViewById(R.id.diabetic_records_1_Medication_compliance_2);


            if (Mc1.isChecked()) {
                String s = Mc1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Medication_compliance  = s;
            } else if(Mc2.isChecked()){
                String s = Mc2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Medication_compliance  = s;
            }else {
                String s = Mc3.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Medication_compliance  = s;
            }
            //classificationText = ((EditText) view .findViewById(R.id.a_postpartum_housecall_classification_abnormalities_text)).getText().toString().trim();
        }
        params.put("tnbFyycx", Medication_compliance);//服药医从性
        //药物不良反应
        String Adverse_reaction_none = "";
        //药物不良反应描述
        String Adverse_reaction_none_ms = "";
        if (view != null) {
            RadioButton Arn1 = view.findViewById(R.id.diabetic_records_1_Adverse_reaction_none);
            RadioButton Arn2 = view.findViewById(R.id.diabetic_records_1_Adverse_reaction_you);

            if (Arn1.isChecked()) {
                String s = Arn1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Adverse_reaction_none  = s;
            } else {
                String s =  Arn2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Adverse_reaction_none  = s;
            }
            Adverse_reaction_none_ms = ((EditText) view .findViewById(R.id.diabetic_records_1_Adverse_reaction_you_text)).getText().toString().trim();
        }
        params.put("tnbYwblfy", Adverse_reaction_none);//药物不良反应
        params.put("tnbYwblfyMs", Adverse_reaction_none_ms);//药物不良反应名称
        //低血糖反应
        String Hypoglycemic_reaction = "";
        if (view != null) {
            RadioButton Hr1 = view.findViewById(R.id.diabetic_records_1_Medication_compliance);
            RadioButton Hr2 = view.findViewById(R.id.diabetic_records_1_Medication_compliance_1);
            RadioButton Hr3 = view.findViewById(R.id.diabetic_records_1_Medication_compliance_2);


            if (Hr1.isChecked()) {
                String s = Hr1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Hypoglycemic_reaction  = s;
            } else if(Hr2.isChecked()){
                String s = Hr2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Hypoglycemic_reaction  = s;
            }else {
                String s = Hr3.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Hypoglycemic_reaction  = s;
            }
            //classificationText = ((EditText) view .findViewById(R.id.a_postpartum_housecall_classification_abnormalities_text)).getText().toString().trim();
        }
        params.put("tnbDxtfy", Hypoglycemic_reaction);//低血唐反映
        //此次随访分类
        String Classification = "";
//        if (((RadioButton) view.findViewById(R.id.diabetic_records_1_Classification)).isChecked()) {
//            //national = "1 汉族";
//            Classification = "1";
//        } else if(((RadioButton) view.findViewById(R.id.diabetic_records_1_Classification_1)).isChecked()) {
//            //national = ((EditText) views[0].findViewById(R.id.archives_national_other)).getText().toString().trim();
//            Classification = "2";
//        }
//        else if(((RadioButton) view.findViewById(R.id.diabetic_records_1_Classification_2)).isChecked()) {
//            //national = ((EditText) views[0].findViewById(R.id.archives_national_other)).getText().toString().trim();
//            Classification = "3";
//        }
//        //分类
//        String classification = "";
//        //分类异常描述
//        String classificationText="";

        if (view != null) {
            RadioButton class1 = view.findViewById(R.id.diabetic_records_1_Classification);
            RadioButton class2 = view.findViewById(R.id.diabetic_records_1_Classification_1);
            RadioButton class3 = view.findViewById(R.id.diabetic_records_1_Classification_2);
            RadioButton class4 = view.findViewById(R.id.diabetic_records_1_Classification_3);

            if (class1.isChecked()) {
                String s = class1.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Classification  = s;
            } else if(class2.isChecked()){
                String s = class2.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Classification  = s;
            }else if(class3.isChecked()){
                String s = class3.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Classification  = s;
            }else {
                String s = class4.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Classification  = s;
            }
            //classificationText = ((EditText) view .findViewById(R.id.a_postpartum_housecall_classification_abnormalities_text)).getText().toString().trim();
        }

        params.put("tnbCcsffl", Classification);//此次随访分类
        mValues.putAll(params);

        return params;
    }
    /**
     * 第五页
     */
    private Map<String, String> Firsttable_4(View view) {
        Map<String, String> params = new HashMap<>();
        // 药物名称1
        String drug_name_1 = "";
        //每日1
        String everyday_1 = "";
        //每次1
        String every_time_1 = "";
        //药物名称2
        String drug_name_2 = "";
        //每日2
        String everyday_2= "";
        //每次2
        String every_time_2= "";
        //药物名称3
        String drug_name_3 = "";
        //每日3
        String everyday_3 = "";
        //每次
        String every_time_3 = "";
        //种类
        String insulin_species = "";
        //用法
        String insulin_dosage = "";
        //用量
        String insulin_dosage_1 = "";
        drug_name_1 = ((EditText) view.findViewById(R.id.diabetic_records_1_drug_name_1)).getText().toString().trim();
        everyday_1 = ((EditText) view.findViewById(R.id.diabetic_records_1_everyday_1)).getText().toString().trim();
        every_time_1 = ((EditText) view.findViewById(R.id.diabetic_records_1_every_time_1)).getText().toString().trim();
        drug_name_2 = ((EditText) view.findViewById(R.id.diabetic_records_1_drug_name_2)).getText().toString().trim();
        everyday_2 = ((EditText) view.findViewById(R.id.diabetic_records_1_everyday_2)).getText().toString().trim();
        every_time_2 = ((EditText) view.findViewById(R.id.diabetic_records_1_every_time_2)).getText().toString().trim();
        drug_name_3= ((EditText) view.findViewById(R.id.diabetic_records_1_drug_name_3)).getText().toString().trim();
        everyday_3  = ((EditText) view.findViewById(R.id.diabetic_records_1_everyday_3)).getText().toString().trim();
        every_time_3 = ((EditText) view.findViewById(R.id.diabetic_records_1_every_time_3)).getText().toString().trim();
        insulin_species = ((EditText) view.findViewById(R.id.diabetic_records_1_insulin_species)).getText().toString().trim();
        insulin_dosage = ((EditText) view.findViewById(R.id.diabetic_records_1_insulin_dosage)).getText().toString().trim();
        insulin_dosage_1 = ((EditText) view.findViewById(R.id.diabetic_records_1_insulin_dosage_1)).getText().toString().trim();

        params.put("tnbYyqkYwmc1", drug_name_1);//药品名称
        params.put("tnbYyqkMejc1", everyday_1);//每次次数
        params.put("tnbYyqkMcjk1", every_time_1);//每次几颗

        params.put("tnbYyqkYwmc2", drug_name_2);
        params.put("tnbYyqkMejc2", everyday_2);
        params.put("tnbYyqkMcjk2", every_time_2);

        params.put("tnbYyqkYwmc3",drug_name_3);
        params.put("tnbYyqkMejc3", everyday_3 );
        params.put("tnbYyqkMcjk3", every_time_3);

        params.put("tnbYyqkYdszl", insulin_species);//胰岛素种类
        params.put("tnbYyqkYdsyf", insulin_dosage);////用法
        params.put("tnbYyqkYdsyl", insulin_dosage_1);//用量
        mValues.putAll(params);
        return params;
    }
    /**
     * 第6页
     */
    private Map<String, String> Firsttable_5(View view) {
        Map<String, String> params = new HashMap<>();
        //转诊
        String zhuanzhen="";
        // 原因
        String reason= "";

        //机构及科室
        String Institutional = "";
        //机构及科室
        String Institutional_1 = "";
        //下次随访日期
        String NextFollowupDate = "";
        //随访医生签名
        String upDoctorSignature = "";
        if (view!= null) {
            RadioButton none = view.findViewById(R.id.diabetic_records_1_TransferTreatment_no);
            RadioButton all = view.findViewById(R.id.diabetic_records_1_TransferTreatment_you);
            if (none.isChecked()) {
                String s = none.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                zhuanzhen = s;
            } else {
                String s = all.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                zhuanzhen = s;
            }
            reason = ((EditText) view.findViewById(R.id.diabetic_records_1_TransferTreatment_yes_reason)).getText().toString().trim();
            Institutional = ((EditText) view.findViewById(R.id.diabetic_records_1_TransferTreatment_yes_Institutional)).getText().toString().trim();
            Institutional_1 = ((EditText) view.findViewById(R.id.diabetic_records_1_TransferTreatment_yes_Institutional_1)).getText().toString().trim();

        }
        NextFollowupDate = ((TextView) view.findViewById(R.id.diabetic_records_1_NextFollowupDate)).getText().toString().trim();
        upDoctorSignature = ((EditText) view.findViewById(R.id.diabetic_records_1_Follow_upDoctorSignature)).getText().toString().trim();
        params.put("zzjy", zhuanzhen);//转诊
        params.put("zzyy", reason);//原因
        params.put("zzjg", Institutional);//机构
        params.put("zzks", Institutional_1);//科室
        params.put("scsfsj", NextFollowupDate);//日期
        params.put("sfYsqm",upDoctorSignature);//签名
        mValues.putAll(params);

        return params;
    }


    /**
     * 随访日期
     */
    private Calendar calendar;
    private DatePickerDialog dialog;

    public void onFollow_up_dateClick1(final View v) {
        calendar = Calendar.getInstance();
        dialog = new DatePickerDialog(TangNiaoBingActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        TextView tv = (TextView) v;
                        tv.setText(year + "-" + (monthOfYear < 10 ? ("0" + monthOfYear) : monthOfYear) + "-"
                                + (dayOfMonth < 10 ? ("0" + dayOfMonth) : dayOfMonth));
                    }
                }, calendar.get(Calendar.YEAR), calendar
                .get(Calendar.MONTH), calendar
                .get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }


    public void onFollow_up_dateClick2(final View v) {
        calendar = Calendar.getInstance();
        dialog = new DatePickerDialog(TangNiaoBingActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        TextView tv = (TextView) v;
                        tv.setText(year + "-" + (monthOfYear < 10 ? ("0" + monthOfYear) : monthOfYear) + "-"
                                + (dayOfMonth < 10 ? ("0" + dayOfMonth) : dayOfMonth));
                    }
                }, calendar.get(Calendar.YEAR), calendar
                .get(Calendar.MONTH), calendar
                .get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
    /**
     * 症状点击事件
     */
    public void onSymptomsClick1(View view) {
        View[] views = getViews(findViewById(R.id.diabetic_records_1_Symptoms));
        CheckBox none = findViewById(R.id.diabetic_records_1_Symptoms_none);
        CheckBox other = findViewById(R.id.diabetic_records_1_Symptoms_other);
        EditText otherText = findViewById(R.id.diabetic_records_1_Symptoms_other_text);
        boolean flag = false;
        for (int i = 0; i < views.length; i++) {
            if (views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) views[i];
                if (cb.isChecked()) {
                    flag = true;
                }
                if (view == none) {
                    if (none.isChecked() && cb != view) {
                        cb.setChecked(false);
                        otherText.setText("");
                        otherText.setEnabled(false);
                    }
                } else {
                    CheckBox v = (CheckBox) view;
                    if (v.isChecked()) {
                        none.setChecked(false);
                    }
                }
            }
        }
        if (flag != true) {
            none.setChecked(true);
        }
        if (other.isChecked()) {
            otherText.setEnabled(true);
            otherText.setFocusable(true);
            otherText.setFocusableInTouchMode(true);
            otherText.requestFocus();
        } else {
            otherText.setEnabled(false);
            otherText.setText("");
        }
    }

    /**
     * 足背动脉搏动点击事件
     */
    public void onPulsationClick1(View view) {
        Spinner sp1=findViewById(R.id.diabetic_records_1_Pulsation_low_xiala);
        Spinner sp2=findViewById(R.id.diabetic_records_1_Pulsation_no_xiala);

        RadioButton rb = (RadioButton) view;
        if (view.getId() == R.id.diabetic_records_1_Pulsation_ok) {
            sp1.setEnabled(false);
            sp2.setEnabled(false);
        } else if (view.getId() == R.id.diabetic_records_1_Pulsation_low) {
            sp1.setEnabled(true);
            sp2.setEnabled(false);
        }else if(view.getId() == R.id.diabetic_records_1_Pulsation_no){
            sp2.setEnabled(true);
            sp1.setEnabled(false);
        }

    }

    /**
     * 药物不良反应点击事件处理
     */
    public void changeAdverse_reactionEnableEdit1(View view) {
        EditText ed = findViewById(R.id.diabetic_records_1_Adverse_reaction_you_text);
        RadioButton rb = (RadioButton) view;
        if (view.getId() == R.id.diabetic_records_1_Adverse_reaction_none) {
            ed.setText("");
            ed.setEnabled(false);
        } else if (rb.isChecked()) {
            ed.setEnabled(true);
            ed.setFocusable(true);
            ed.setFocusableInTouchMode(true);
            ed.requestFocus();
        }
    }

    /**
     * 下次随访日期
     */

    public void onXiacisuifangriqiClick1(final View v) {
        calendar = Calendar.getInstance();
        dialog = new DatePickerDialog(TangNiaoBingActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        TextView tv = (TextView) v;
                        String month;
                        String day;
                        if (monthOfYear <9){
                            month = "0"+(monthOfYear+1);
                        }else {
                            month = ""+(monthOfYear+1);
                        }
                        if (dayOfMonth <10){
                            day = "0"+dayOfMonth;
                        }else {
                            day = dayOfMonth + "";
                        }
                        tv.setText(year + "-" + month  + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar
                .get(Calendar.MONTH), calendar
                .get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
    /*转诊*/
    public void onTransferTreatmentClick1(View view) {
        EditText ed1 = findViewById(R.id.diabetic_records_1_TransferTreatment_yes_reason);
        EditText ed2 = findViewById(R.id.diabetic_records_1_TransferTreatment_yes_Institutional);
        EditText ed3 = findViewById(R.id.diabetic_records_1_TransferTreatment_yes_Institutional_1);
        RadioButton rb = (RadioButton) view;
        if (view.getId() == R.id.diabetic_records_1_TransferTreatment_no) {
            ed1.setText("");
            ed1.setEnabled(false);
            ed2.setText("");
            ed2.setEnabled(false);
            ed3.setText("");
            ed3.setEnabled(false);
        } else if (rb.isChecked()) {
            ed1.setEnabled(true);
            ed1.setFocusable(true);
            ed1.setFocusableInTouchMode(true);
            ed1.requestFocus();
            ed2.setEnabled(true);
            ed2.setFocusable(true);
            ed2.setFocusableInTouchMode(true);
            ed2.requestFocus();
            ed3.setEnabled(true);
            ed3.setFocusable(true);
            ed3.setFocusableInTouchMode(true);
            ed3.requestFocus();


        }
    }

    /**
     * 检查日期
     */

    public void onDoeClick1(final View v) {
        calendar = Calendar.getInstance();
        dialog = new DatePickerDialog(TangNiaoBingActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        TextView tv = (TextView) v;
                        String month;
                        String day;
                        if (monthOfYear <9){
                            month = "0"+(monthOfYear+1);
                        }else {
                            month = ""+(monthOfYear+1);
                        }
                        if (dayOfMonth <10){
                            day = "0"+dayOfMonth;
                        }else {
                            day = dayOfMonth + "";
                        }
                        tv.setText(year + "-" + month  + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar
                .get(Calendar.MONTH), calendar
                .get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }


    /**
     * Toolbar的事件---返回
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 获取子视图
     */
    private View[] views;

    private View[] getViews(View view) {
        views = new View[0];
        parseView(view);
        return views;
    }

    private void parseView(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) view;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View v = vg.getChildAt(i);
                parseView(v);
            }
        } else {
            views = Arrays.copyOf(views, views.length + 1);
            views[views.length - 1] = view;
        }
    }


    /**
     * 提交数据
     */
    public void onClickSubmit(View view) {
        showLoading();
        int count = expandableListView.getExpandableListAdapter().getGroupCount();
        for (int i = 0; i < count; i++) {
            expandableListView.collapseGroup(i);
        }
        Observable.create(new ObservableOnSubscribe<DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<DataBean> emitter) throws Exception {
                JSONObject jsonObject = new JSONObject(mValues);
                MyLogger.i(jsonObject.toString());
                String json = OkgoNetwork.post(url,jsonObject.toString());
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, DataBean.class));

            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<DataBean>() {
            @Override
            public void onNext(DataBean dataBean) {
                  if (dataBean.getCode()==0){
                      MyToast.show("提交成功");
                      finish();
                  }else {
                      MyToast.show(dataBean.getMsg());
                  }
                  dismissLoading();

            }
        });

    }


    //新
    private Map<Object,Object> mValues = new HashMap<>();
    private  String url = Constant.baseurl+"online/extnb/save";
    private LoadingViewDialog mLoadingView;
    //整合专项  基础 修改
    private void initBasic(){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Toolbar toolbar = (Toolbar)findViewById(com.oracle.children.R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mValues.put("token", User.getToken());
        mValues.put("jmId",getIntent().getIntExtra("jmid", 0) );
        mValues.put("userId", User.getUserId());//社区医生id;
        mValues.put("mark", 1);
        //随访id
        int id = getIntent().getIntExtra("id", 0);
        if (id !=0){
            mValues.put("sfjlId", id);
            mValues.put("id", id);
        }
    }



    public void showLoading(){
        mLoadingView = new LoadingViewDialog();
        mLoadingView.show(getSupportFragmentManager(),"" );
    }
    public void dismissLoading(){
        if (mLoadingView != null){
            mLoadingView.dismiss();
        }

    }

}
